// @ts-nocheck

// 待触发 `UniAppJSBridgeReady` 事件后，即可调用 uni 的 API。
document.addEventListener('UniAppJSBridgeReady', function () {
  new GoogleMap()
});


class GoogleMap {
  key = 'AIzaSyCmgZ0AF731KrqicjMwoPuRh946hIjIHTw'
  map = null
  markers = []
  polylines = []
  constructor() {
    this.loadMap()
    this.getList()
  }
  /**获取地址栏传递参数 */
  getQueryParams() {
    // 获取当前页面的 URL
    const url = window.location.href;
    // 解析 URL 中的参数
    const urlParams = new URLSearchParams(new URL(url).search);
    const query = JSON.parse(urlParams.get('query') || "{}")
    const config = JSON.parse(urlParams.get('config') || "{}")
    const userStore = JSON.parse(urlParams.get('userStore') || "{}")
    return { query, config, userStore }
  }
  async loadMap() {
    // https://developers.google.com/maps/documentation/javascript/load-maps-js-api?hl=zh-cn
    await (g => {
      var h, a, k, p = "The Google Maps JavaScript API",
        c = "google",
        l = "importLibrary",
        q = "__ib__",
        m = document,
        b = window;
      b = b[c] || (b[c] = {});
      var d = b.maps || (b.maps = {}),
        r = new Set,
        e = new URLSearchParams,
        u = () => h || (h = new Promise(async (f, n) => {
          await (a = m.createElement("script"));
          e.set("libraries", [...r] + "");
          for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]);
          e.set("callback", c + ".maps." + q);
          a.src = `https://maps.${c}apis.com/maps/api/js?` + e;
          d[q] = f;
          a.onerror = () => h = n(Error(p + " could not load."));
          a.nonce = m.querySelector("script[nonce]")?.nonce || "";
          m.head.append(a)
        }));
      d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[
        l](f, ...n))
    })({
      key: this.key,
      v: "weekly",
      // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.).
      // Add other bootstrap parameters as needed, using camel case.
    });
    this.initMap();
  }
  async initMap() {
    const {
      Map
    } = await google.maps.importLibrary("maps");

    this.map = new Map(document.getElementById("map"), {
      zoom: 10,
      mapId: 'googleMap_ID',
      center: {
        lat: 35.736156,
        lng: 139.714222
      },
      fullscreenControl: false, // 隐藏全屏控件
      streetViewControl: false, // 隐藏街景控件
      zoomControl: false, // 隐藏缩放控件
      mapTypeControl: false, // 隐藏地图类型控件
    });
  }
  resetMap() {
    // 要清空谷歌地图上的所有线（Polyline）和标记（Marker）
    this.markers.map((m) => m.setMap(null))
    this.markers = []
    this.polylines.map((p) => p.setMap(null))
    this.polylines = []
  }
  getList() {
    const q = this.getQueryParams()
    console.log(`1 ==>`, 1);
    if (!routeId) return
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => {
        console.log(data);
      })
      .catch(error => {
        console.error(error);
      });
    myList({ routeId, status: this.status }).then((res) => {
      this.tableData = res.rows.map((item) => {
        return {
          ...item,
          _uuid: uuid(),
          // <!--status 1.待配送 2.配送中 3.已完成 -->
          show: item.status != 3,
        }
      })
    })
  }
}